package com.web.weixin.user.group.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.web.common.util.JsonUtil;
import com.web.weixin.kit.SendKit;
import com.web.weixin.user.group.WeixinGroup;
import com.web.weixin.user.group.service.IWeixinGroupService;
import com.web.weixin.util.WeixinConstant;

import net.sf.json.JSONObject;
@SuppressWarnings({ "unchecked", "rawtypes" })
@Service
public class WeixinGroupService implements IWeixinGroupService {

	@Override
	public WeixinGroup addWeixinGroup(WeixinGroup group) {
		Map<String,WeixinGroup> map=new HashMap<String,WeixinGroup>();
		map.put("group", group);
		String json=JsonUtil.objectToJsonStr(map);
		String contentType="application/json";
		String url=WeixinConstant.CREATE_WEIXIN_GROUP;
		url=url.replaceAll("ACCESS_TOKEN", WeixinConstant.ACCESS_TOKEN);
		String result=SendKit.sendPost(url, contentType, json);
		Map<String,Object> wgs=(Map<String,Object>)JsonUtil.fromJson(result, Map.class);
		Object o=wgs.get("group");
		JSONObject jso=JSONObject.fromObject(o);
		WeixinGroup wg=(WeixinGroup)JSONObject.toBean(jso, WeixinGroup.class);
		return wg;
	}

	
	@Override
	public List<WeixinGroup> queryAllWeixinGroup() {
		try {
			String url=WeixinConstant.QUERY_ALL_WEIXIN_GROUP;
			url=url.replaceAll("ACCESS_TOKEN", WeixinConstant.ACCESS_TOKEN);
			String result=SendKit.sendGet(url);
			Map<String,List> wgs=(Map<String,List>)JsonUtil.fromJson(result, Map.class);
			System.out.println(wgs);
			List<Object> wgls=(List<Object>)wgs.get("groups");
			List<WeixinGroup> wglist=new ArrayList<WeixinGroup>();
			for(Object o:wgls){
				JSONObject jso=JSONObject.fromObject(o);
				WeixinGroup wg=(WeixinGroup)JSONObject.toBean(jso, WeixinGroup.class);
				wg.getName();
				wglist.add(wg);
			}
			return wglist;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String queryWeixinUserGroup(String openId) {
		String url=WeixinConstant.QUERY_WEIXIN_USER_GROUP;
		url=url.replaceAll("ACCESS_TOKEN", WeixinConstant.ACCESS_TOKEN);
		Map<String,String> omp=new HashMap<String, String>();
		omp.put("openid", openId);
		String data=JsonUtil.simpleMapToJsonStr(omp);
		String contentType="application/json";
		String json=SendKit.sendPost(url, contentType, data);
		Map<String,Object> rt=JsonUtil.jsonToMap(json);
		String grpId=rt.get("groupid").toString();
		return grpId;
	}

	@Override
	public void updateWeixinGroupName(int groupId, String name) {
		// TODO Auto-generated method stub

	}

	@Override
	public void moveWeixinUserToGroup(String openId, int groupId) {
		// TODO Auto-generated method stub

	}

	@Override
	public void moveWeixinUserToGroup(List<String> openIds, int groupId) {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteWeixinGroup(int groupId) {
		// TODO Auto-generated method stub

	}

}
